ALTER     PROC sp_save_po_lot_info(
 @poNumber VARCHAR(17), 
 @partNumber VARCHAR(50),
 @lotNumber VARCHAR(21),
 @lotId INT,
 @lotQty INT,
 @poSeqNumber INT,
 @ps_type VARCHAR(50),
 @ps_type_name VARCHAR(50),
 @vendorName VARCHAR(50),
 @vendorNumber VARCHAR(15)
 ) 
 AS 
 BEGIN
 	DECLARE @itemNumber VARCHAR(31)
 	DECLARE @locationCode VARCHAR(11)
	DECLARE @previousQty INT
 
 	EXEC @itemNumber = fn_ct_get_gpItemNumber @partNumber, @ps_type, @ps_type_name
 	SET @locationCode = SUBSTRING( LTRIM( RTRIM( @vendorName ) ), 1, 7 ) + '/' + SUBSTRING( @ps_type, 1, 3 )
 	SET @poSeqNumber = @poSeqNumber * 16384

	
	SELECT @previousQty = ISNULL ( QTYRECVD, 0 ) FROM Auto_Invoice_PO WHERE 
	PONUMBER = @poNumber AND PO_ORD = @poSeqNumber AND ITEMNMBR = @itemNumber AND VENDORID = @vendorNumber 
	AND LOTID = @lotId AND POPRCTNM IS NULL 

	IF ( ( @previousQty IS NULL ) OR ( @previousQty = 0 ) ) 
	BEGIN
	 	INSERT INTO Auto_Invoice_PO( PONUMBER, PO_ORD, ITEMNMBR, VENDORID, LOCNCODE, LOTID, LOTNUMBER, QTYRECVD ) VALUES 
 		( @poNumber, @poSeqNumber, @itemNumber, @vendorNumber, @locationCode, @lotId, @lotNumber, @lotQty )
	END
	ELSE
	BEGIN
		UPDATE Auto_Invoice_PO SET QTYRECVD = QTYRECVD + @lotQty WHERE 
		PONUMBER = @poNumber AND PO_ORD = @poSeqNumber AND ITEMNMBR = @itemNumber AND VENDORID = @vendorNumber 
		AND LOTID = @lotId AND POPRCTNM IS NULL 
	END
 END